package stack

import "fmt"

// 链式存储的栈--链栈

type StackNode struct {
	Data interface{}
	Next *StackNode
}

func NewStackNode(data interface{}) *StackNode {
	return &StackNode{Data: data, Next: nil}
}

type LinkStack struct {
	Top *StackNode
}

func NewLinkStack() *LinkStack {
	return &LinkStack{Top: NewStackNode(nil)}
}

func (ls *LinkStack) IsEmpty() bool {
	return ls.Top.Next == nil
}

func (ls *LinkStack) PushStack(data interface{}) {
	node := NewStackNode(data)
	node.Next = ls.Top.Next
	ls.Top.Next = node
}

func (ls *LinkStack) TravelLinkStack() {
	if ls.IsEmpty() {
		fmt.Println("链栈为空")
		return
	}
	fmt.Printf("当前链栈元素： ")
	cNode := ls.Top.Next
	for cNode != nil {
		fmt.Printf("%v, ", cNode.Data)
		cNode = cNode.Next
	}
	fmt.Println()
}

func (ls *LinkStack) PopStack() *StackNode {
	if ls.IsEmpty() {
		return nil
	}
	topNode := ls.Top.Next
	ls.Top.Next = ls.Top.Next.Next
	return topNode
}

func (ls *LinkStack) GetTopStack() *StackNode {
	if ls.IsEmpty() {
		return nil
	}
	topNode := ls.Top.Next
	return topNode
}
